<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>三色标记算法：现代垃圾回收的艺术</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-shadow {
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
            transition: all 0.3s ease;
        }
        
        .card-shadow:hover {
            transform: translateY(-5px);
            box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
        }
        
        .color-circle {
            width: 60px;
            height: 60px;
            border-radius: 50%;
            display: inline-block;
            margin: 0 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
            transition: transform 0.3s ease;
        }
        
        .color-circle:hover {
            transform: scale(1.1);
        }
        
        .white-circle {
            background: #ffffff;
            border: 2px solid #e0e0e0;
        }
        
        .gray-circle {
            background: #9e9e9e;
        }
        
        .black-circle {
            background: #212121;
        }
        
        .step-card {
            position: relative;
            overflow: hidden;
        }
        
        .step-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 5px;
            height: 100%;
            background: linear-gradient(180deg, #667eea 0%, #764ba2 100%);
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .section-title {
            position: relative;
            display: inline-block;
            margin-bottom: 2rem;
        }
        
        .section-title::after {
            content: '';
            position: absolute;
            bottom: -10px;
            left: 0;
            width: 60px;
            height: 4px;
            background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .mermaid {
            background: white;
            padding: 2rem;
            border-radius: 1rem;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
        }
        
        .feature-icon {
            width: 50px;
            height: 50px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 12px;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
            font-size: 1.5rem;
            margin-bottom: 1rem;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fadeInUp {
            animation: fadeInUp 0.8s ease-out;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center animate-fadeInUp">
            <h1 class="text-5xl md:text-6xl font-bold mb-6" style="font-family: 'Noto Serif SC', serif;">
                三色标记算法
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">
                现代垃圾回收的艺术与科学
            </p>
            <div class="flex justify-center items-center space-x-4">
                <div class="color-circle white-circle"></div>
                <i class="fas fa-arrow-right text-2xl"></i>
                <div class="color-circle gray-circle"></div>
                <i class="fas fa-arrow-right text-2xl"></i>
                <div class="color-circle black-circle"></div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction Card -->
        <div class="bg-white rounded-2xl p-8 mb-12 card-shadow">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">三</span>色标记算法（Three-Color Marking Algorithm）是一种用于垃圾回收的算法，主要用于解决对象的垃圾回收和内存管理问题。它是标记-清除（Mark-and-Sweep）垃圾回收算法的一种变体，用于高效地识别和回收垃圾对象。三色标记算法通过将对象分为三种颜色来跟踪对象的状态，从而实现准确的垃圾回收。
            </p>
        </div>

        <!-- Core Concept Section -->
        <section class="mb-16">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-8">
                <i class="fas fa-lightbulb mr-3 text-purple-600"></i>核心思想
            </h2>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white rounded-xl p-6 card-shadow text-center">
                    <div class="color-circle white-circle mx-auto mb-4"></div>
                    <h3 class="text-xl font-bold mb-3">白色 White</h3>
                    <p class="text-gray-600">表示对象是未标记的，可能是垃圾对象，需要在垃圾回收过程中被回收。</p>
                </div>
                
                <div class="bg-white rounded-xl p-6 card-shadow text-center">
                    <div class="color-circle gray-circle mx-auto mb-4"></div>
                    <h3 class="text-xl font-bold mb-3">灰色 Gray</h3>
                    <p class="text-gray-600">表示对象已经被标记，但其引用的对象尚未被标记。灰色对象是当前正在处理的对象。</p>
                </div>
                
                <div class="bg-white rounded-xl p-6 card-shadow text-center">
                    <div class="color-circle black-circle mx-auto mb-4"></div>
                    <h3 class="text-xl font-bold mb-3 text-gray-800">黑色 Black</h3>
                    <p class="text-gray-600">表示对象已经被标记，并且它引用的对象也都已经被标记。黑色对象及其引用的对象都被认为是存活的。</p>
                </div>
            </div>
        </section>

        <!-- Workflow Section -->
        <section class="mb-16">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-8">
                <i class="fas fa-cogs mr-3 text-purple-600"></i>工作流程
            </h2>
            
            <div class="space-y-6">
                <div class="bg-white rounded-xl p-6 step-card card-shadow">
                    <div class="flex items-start">
                        <div class="feature-icon mr-4">
                            <span class="font-bold">1</span>
                        </div>
                        <div>
                            <h3 class="text-xl font-bold mb-2">初始标记 Initial Marking</h3>
                            <p class="text-gray-600 leading-relaxed">从根对象（GC Root）开始，将所有直接可达的对象标记为灰色。根对象通常包括栈中的引用、静态字段、线程中的本地变量等。这个阶段是非并发的，通常需要暂停应用程序的执行。</p>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl p-6 step-card card-shadow">
                    <div class="flex items-start">
                        <div class="feature-icon mr-4">
                            <span class="font-bold">2</span>
                        </div>
                        <div>
                            <h3 class="text-xl font-bold mb-2">标记传播 Propagation</h3>
                            <p class="text-gray-600 leading-relaxed">遍历所有灰色对象，将它们的所有引用对象标记为灰色，并将自己标记为黑色。然后将这些新标记为灰色的对象加入待处理队列。这个阶段可以并发进行，即灰色对象的处理和新的灰色对象的标记可以并发执行。</p>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl p-6 step-card card-shadow">
                    <div class="flex items-start">
                        <div class="feature-icon mr-4">
                            <span class="font-bold">3</span>
                        </div>
                        <div>
                            <h3 class="text-xl font-bold mb-2">标记完成 Final Marking</h3>
                            <p class="text-gray-600 leading-relaxed">当所有灰色对象的引用对象都被标记为灰色并处理完成时，这些灰色对象将被标记为黑色。待处理队列为空时，所有可达对象已经标记完毕。这个阶段可以是非并发的，也可以是并发的，取决于具体的垃圾回收器实现。</p>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl p-6 step-card card-shadow">
                    <div class="flex items-start">
                        <div class="feature-icon mr-4">
                            <span class="font-bold">4</span>
                        </div>
                        <div>
                            <h3 class="text-xl font-bold mb-2">清除未标记对象 Sweep</h3>
                            <p class="text-gray-600 leading-relaxed">在标记完成后，所有仍然是白色的对象被认为是不可达的垃圾，将被回收。这一阶段主要负责释放内存。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Visualization Section -->
        <section class="mb-16">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-8">
                <i class="fas fa-project-diagram mr-3 text-purple-600"></i>算法流程可视化
            </h2>
            
            <div class="mermaid">
                graph TD
                    A[GC Root] -->|初始标记| B[灰色对象队列]
                    B -->|标记传播| C{检查引用}
                    C -->|有未标记引用| D[标记引用为灰色]
                    C -->|无未标记引用| E[标记自身为黑色]
                    D --> B
                    E --> F{队列是否为空}
                    F -->|否| B
                    F -->|是| G[标记完成]
                    G --> H[清除白色对象]
                    H --> I[垃圾回收完成]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:2px,color:#fff
                    style B fill:#9e9e9e,stroke:#fff,stroke-width:2px,color:#fff
                    style E fill:#212121,stroke:#fff,stroke-width:2px,color:#fff
                    style I fill:#764ba2,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </section>

        <!-- Pros and Cons Section -->
        <section class="mb-16">
            <h2 class="section-title text-3xl font-bold text-gray-800 mb-8">
                <i class="fas fa-balance-scale mr-3 text-purple-600"></i>优缺点分析
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-gradient-to-br from-green-50 to-green-100 rounded-xl p-6 card-shadow">
                    <h3 class="text-2xl font-bold mb-4 text-green-800">
                        <i class="fas fa-check-circle mr-2"></i>优点
                    </h3>
                    <ul class="space-y-3">
                        <li class="flex items-start">
                            <i class="fas fa-chevron-right text-green-600 mt-1 mr-3"></i>
                            <div>
                                <strong class="text-green-800">准确性：</strong>
                                <span class="text-gray-700">通过灰色对象的传播，三色标记算法能准确地标记出所有存活的对象，减少了误回收的可能性。